﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Interactivity;
using System.Windows.Shapes;

namespace JHMVVMFramework.Common.Behavior
{
    public class VerticalRichTextBlockBehavior : Behavior<RichTextBox>
    {
        private static string REGULAR_STRING = "[\u4e00-\u9fa5]";

        public static DependencyProperty ContentProperty
            = DependencyProperty.RegisterAttached(
                "Content", typeof(string),
                typeof(VerticalRichTextBlockBehavior),
                new PropertyMetadata(null, OnContentChanged));


        public string Content
        {
            set
            {
                SetValue(ContentProperty, value);
            }
            get
            {
                return (string)GetValue(ContentProperty);
            }
        }

        bool m_isNeedProcess = false;
        string m_tempContent;

        public VerticalRichTextBlockBehavior()
        { }

        protected override void OnAttached()
        {
            base.OnAttached();
            if (m_isNeedProcess)
            {
                m_isNeedProcess = false;
                VericalContent(m_tempContent);
            }
        }


        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string content = e.NewValue as string;

            VerticalRichTextBlockBehavior sender = d as VerticalRichTextBlockBehavior;


            if (!string.IsNullOrWhiteSpace(content) && sender != null)
            {
                if (sender.AssociatedObject == null)
                {
                    sender.m_isNeedProcess = true;
                    sender.m_tempContent = content;
                }
                else
                {
                    sender.VericalContent(content);
                }
            }
        }

        void VericalContent(string content)
        {
            if (string.IsNullOrWhiteSpace(Content))
                return;
            Regex regex = new Regex(REGULAR_STRING);
            Paragraph rootParagraph = new Paragraph();

            this.AssociatedObject.Blocks.Clear();
            this.AssociatedObject.Blocks.Add(rootParagraph);

            for (int i = 0; i < content.Length; i++)
            {
                if (regex.IsMatch(content[i].ToString()))
                {
                    InlineUIContainer inlineUIContiner = new InlineUIContainer();
                    TextBlock tb = new TextBlock();
                    tb.Text = content[i].ToString();
                    tb.RenderTransformOrigin = new Point(0.5, 0.5);
                    if (this.AssociatedObject.RenderTransform as CompositeTransform != null)
                    {
                        tb.RenderTransform = new CompositeTransform() { Rotation = (-(this.AssociatedObject.RenderTransform as CompositeTransform).Rotation) };
                    }
                    rootParagraph.Inlines.Add(inlineUIContiner);
                    inlineUIContiner.Child = tb;
                }
                else
                {
                    rootParagraph.Inlines.Add(new Run() { Text = content[i].ToString() });
                }
            }


        }
    }
}
